<template>
  <el-container>
    <el-header>
      <a href="/"><img :src="picturePath" alt=""></a>
    </el-header>
  <div class="loginContainer">
    <el-card shadow="always">
      <div class="pic"></div>
      <div class="text-item">
        <el-form
          :model="formData"
          status-icon
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item>
            <h2>用户注册</h2>
          </el-form-item>
          <el-form-item label="用户名" prop="username">
            <el-input
              type="text"
              v-model="formData.username"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input
              type="password"
              v-model="formData.password"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="checkPassword">
            <el-input
              type="password"
              v-model="formData.checkPassword"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-link href="/Login" type="primary"
              >已有账号,登录</el-link
            >
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')"
              >注册</el-button
            >
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
  </el-container>
</template>

<script>
import {register} from "../api/user"
import { getWebInfo } from "../api/web";
import common from "../util/common"
export default {
  data() {
    return {
            localhostPath:common.localhostPath,
      picturePath:"",
      formData: {
        username: "",
        password: "",
        checkPassword:""
      },
      rules: {
        // required 选择必填 trigger: 'blur' blur失去焦点
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        checkPassword: [{ required: true, message: "请输入确认密码", trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
            register(this.formData)
              .then(({ data }) => {
                if (data.code === 200) {
            
                  this.$router.push("/Login");
                } else {
                  this.$message({ type: "error", message: data.msg });
                }
              })
              .catch((err) => {
                console.log(err);
              });
          } else {
            console.log("error submit!!");
            return false;
        }
      });
    },
    //重置
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
    mounted(){
    getWebInfo().then(({data})=>{
      this.picturePath = this.localhostPath + data.data.logoPath;
    })
  }
};
</script>

<style scoped>
.loginContainer {
  width: 100vw;
  height:calc(100vh - 80px);
  display: flex;
  justify-content: center;
  align-items: center;
}
.el-card {
  width: 900px;
  height: 450px;
}
.pic {
  background: url("../assets/DataEntry.png") no-repeat;
  width: 600px;
  height: 450px;
  position: absolute;
}
.text-item {
  position: relative;
  width: 300px;
  height: 450px;
  /* display: flex; */
  /* align-items: center; */
  margin-left: 500px;
}
.el-link {
  display: inline;
}
.el-form-item__content {
  line-height: 0px;
}
</style>